<template>
    <div class="app-container">
        <div class="search-container">
            <el-input
                placeholder="请输入搜索内容，左侧选择该分类等级"
                v-model="keyword"
                class="input-with-select search"
            >
                <el-select v-model="selectRange" slot="prepend" placeholder="请选择等级" :style="{ width: '70px' }">
                    <el-option label="1" value="1"></el-option>
                    <el-option label="2" value="2"></el-option>
                    <el-option label="3" value="3"></el-option>
                    <el-option label="4" value="4"></el-option>
                    <el-option label="5" value="5"></el-option>
                </el-select>
            </el-input>
            <el-button type="primary" class="button" @click="handleAddType">添加</el-button>
        </div>
        <!-- 文章分类表格 -->
        <el-table :data="data" style="width: 100%" border v-loading.fullscreen="listLoading">
            <el-table-column label="序号" min-width="60" align="center">
                <template slot-scope="scope">
                    {{ scope.$index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="分类名称" min-width="180" align="center">
                <template slot-scope="scope">
                    {{ scope.row.name }}
                </template>
            </el-table-column>
            <el-table-column label="文章数量" align="center">
                <template slot-scope="scope">
                    {{ scope.row.articleCount }}
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-tooltip content="编辑" placement="top">
                        <el-button
                            @click="handleEditType(scope.row)"
                            type="primary"
                            icon="el-icon-edit"
                            circle
                            size="mini"
                        ></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top">
                        <el-button
                            @click="handleDeleteType(scope.row)"
                            type="danger"
                            icon="el-icon-delete"
                            circle
                            size="mini"
                        ></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="编辑文章分类" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="分类名称">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="排序等级">
                    <el-select v-model="form.order" placeholder="请选择活动区域">
                        <el-option label="1" value="1"></el-option>
                        <el-option label="2" value="2"></el-option>
                        <el-option label="3" value="3"></el-option>
                        <el-option label="4" value="4"></el-option>
                        <el-option label="5" value="5"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleDialogConfirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getBlogType, addBlogType, editType, deleteType, findOneBlogType } from "@/api/blog"
export default {
    data() {
        return {
            data: [],
            keyword: "",
            selectRange: "1",
            listLoading: false, // 暂时没添加
            dialogFormVisible: false,
            form: {
                id: "",
                name: "",
                order: ""
            }
        }
    },
    created() {
        this.fetchData()
    },
    methods: {
        async fetchData() {
            this.listLoading = true
            const res = await getBlogType()
            this.listLoading = false
            this.data = res.data
        },
        async handleEditType({ id, name, order }) {
            this.dialogFormVisible = true
            this.form.id = id
            this.form.name = name
            this.form.order = order
        },
        async handleDeleteType({ id }) {
            this.$confirm("将删除文章分类, 是否继续?", "确认是否删除", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(async () => {
                    await deleteType(id)
                    this.fetchData()
                    this.$message.success("删除成功")
                })
                .catch(() => {
                    this.$message.info("取消删除")
                })
        },
        // 添加文章分类
        async handleAddType() {
            this.keyword = this.keyword.trim()
            if (!this.keyword) {
                this.$message.error("请输入文字分类名称后添加")
                return
            }
            await addBlogType({ name: this.keyword, order: +this.selectRange })
            this.fetchData()
            this.keyword = ""
            this.$message.success("添加分类成功")
        },
        // 编辑文字确认
        async handleDialogConfirm() {
            const { name, order } = this.form
            await editType({ name, order }, this.form.id)
            this.fetchData()
            this.$message.success("修改文章分类")
            this.dialogFormVisible = false
        }
    }
}
</script>

<style scoped>
.search-container {
    margin: 10px 0;
}
.search {
    width: 400px;
}
.button {
    margin-left: 15px;
}
</style>
